<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人" prop="legalName">
        <el-input
          v-model="queryParams.legalName"
          placeholder="请输入法人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="财务联系人" prop="accontingName">
        <el-input
          v-model="queryParams.accontingName"
          placeholder="请输入财务联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司性质" prop="taxType">
        <el-select v-model="queryParams.taxType" placeholder="请选择公司性质" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_tax_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司序号" align="center" prop="companyInfoId" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="法人" align="center" prop="legalName" />
      <el-table-column label="法人身份证号" align="center" prop="legalId" />
      <el-table-column label="纳税税号" align="center" prop="taxId" />
      <el-table-column label="地址" align="center" prop="companyAddress" />
      <el-table-column label="成立日期" align="center" prop="setDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.setDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册资本" align="center" prop="capital" />
      <el-table-column label="财务联系人" align="center" prop="accontingName" />
      <el-table-column label="公司性质" align="center" prop="taxType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_tax_type" :value="scope.row.taxType"/>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="法人" prop="legalName">
          <el-input v-model="form.legalName" placeholder="请输入法人" />
        </el-form-item>
        <el-form-item label="法人身份证号" prop="legalId">
          <el-input v-model="form.legalId" placeholder="请输入法人身份证号" />
        </el-form-item>
        <el-form-item label="纳税税号" prop="taxId">
          <el-input v-model="form.taxId" placeholder="请输入纳税税号" />
        </el-form-item>
        <el-form-item label="地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="成立日期" prop="setDate">
          <el-date-picker clearable size="small"
            v-model="form.setDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择成立日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注册资本" prop="capital">
          <el-input v-model="form.capital" placeholder="请输入注册资本" />
        </el-form-item>
        <el-form-item label="财务联系人" prop="accontingName">
          <el-input v-model="form.accontingName" placeholder="请输入财务联系人" />
        </el-form-item>
        <el-form-item label="公司性质" prop="taxType">
          <el-select v-model="form.taxType" placeholder="请选择公司性质">
            <el-option
              v-for="dict in dict.type.sys_tax_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/system/info";

export default {
  name: "Info",
  dicts: ['sys_tax_type'],
  data() {
    let checkRegNo = (rule,value,callback) => {
      var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
      var tip = "";
      var pass= true;

      var patt1=new RegExp("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)");
      if(value == ''){
        pass = true;
      }else if(!patt1.test(value)){
        callback(new Error("身份证号格式错误"));
        pass = false;
      } else if(!city[value.substr(0,2)]){
        callback(new Error("地址编码错误"));
        pass = false;
      }else{
        //18位身份证需要验证最后一位校验位
        if(value.length == 18){
          value = value.split('');
          //∑(ai×Wi)(mod 11)
          //加权因子
          var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
          //校验位
          var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
          var sum = 0;
          var ai = 0;
          var wi = 0;
          for (var i = 0; i < 17; i++)
          {
            ai = value[i];
            wi = factor[i];
            sum += ai * wi;
          }
          var last = parity[sum % 11];
          if(parity[sum % 11] != value[17]){
            callback(new Error("身份证号码错误"));
            pass =false;
          }
        }
      }
      if(pass){
        callback();
      }

    }


    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司信息管理表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        legalName: null,
        accontingName: null,
        taxType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        legalId: [
          { validator: checkRegNo, trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公司信息管理列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyInfoId: null,
        companyName: null,
        legalName: null,
        legalId: null,
        taxId: null,
        companyAddress: null,
        setDate: null,
        capital: null,
        accontingName: null,
        taxType: null,
        basicacountId: null,
        generalacount1Id: null,
        generalacount2Id: null,
        creatBy: null,
        creatTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.companyInfoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const companyInfoId = row.companyInfoId || this.ids
      getInfo(companyInfoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companyInfoId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyInfoIds = row.companyInfoId || this.ids;
      this.$modal.confirm('是否确认删除公司信息管理编号为"' + companyInfoIds + '"的数据项？').then(function() {
        return delInfo(companyInfoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
